public without sharing class IntentionalProductMonthFollowHandler {
    
    /*
	当新建/编辑业务机会月度跟进情况、意向产品月度跟进情况时，按照年份+月份跟当前日期进行对比，标记距离当前日期最近的跟进情况为最近一次跟进情况。

*/

    public static void lastFollowup(List<IntentionalProductMonthFollow__c> list_new){

        for(IntentionalProductMonthFollow__c omf :list_new){
            omf.LastFollowup__c = true;
        }

    }

    public static void cancelLastFollowup(List<IntentionalProductMonthFollow__c> list_new){
        Set<Id> intentSet = new Set<Id>();
        Set<Id> omfset = new Set<Id>();
        for(IntentionalProductMonthFollow__c omf :list_new){
            if(omf.IntentProduct__c != null){
                intentSet.add(omf.IntentProduct__c);
                omfset.add(omf.Id);
            }
        }
        List<IntentionalProductMonthFollow__c> tmp = new List<IntentionalProductMonthFollow__c>();
        for(IntentionalProductMonthFollow__c omf : [select Id ,IntentProduct__c 
            from IntentionalProductMonthFollow__c where IntentProduct__c in:intentSet and LastFollowup__c = true   ]){
            if(!omfset.contains(omf.Id))tmp.add(new IntentionalProductMonthFollow__c(Id= omf.Id ,LastFollowup__c = false));
        }
        if(!tmp.isEmpty())update tmp;

    }
    /*
    public static void lastFollowup(List<IntentionalProductMonthFollow__c> list_new,Map<Id,IntentionalProductMonthFollow__c> oldMap  ){

    	Set<Id> oppset  = new Set<Id>();

    	for(IntentionalProductMonthFollow__c omf : list_new){
            IntentionalProductMonthFollow__c old = oldMap == null ? null : oldMap.get(omf.Id );
    		if(omf.IntentProduct__c != null &&  (old == null || omf.LastFollowup__c == old.LastFollowup__c)  )oppset.add(omf.IntentProduct__c);
    	}
    	if( oppset.size() == 0)return;

    	Map<Id,List<IntentionalProductMonthFollow__c>> omfMap = new Map<Id,List<IntentionalProductMonthFollow__c>>();
    	for(IntentionalProductMonthFollow__c omf : [select Year__c , Month__c , IntentProduct__c ,LastFollowup__c 
    		from IntentionalProductMonthFollow__c where IntentProduct__c in: oppset  Order ]){
    		
    		List<IntentionalProductMonthFollow__c> tmp = omfMap.containsKey(omf.IntentProduct__c) ? omfMap.get(omf.IntentProduct__c) : new List<IntentionalProductMonthFollow__c>(); 
    		tmp.add(omf);
    		omfMap.put(omf.IntentProduct__c,tmp );
    	}
    	Set<Id>newomfset = new Set<Id>();
    	List<IntentionalProductMonthFollow__c> omfList = new List<IntentionalProductMonthFollow__c>();
    	 Set<Id> omfset = new Set<Id>();

    	for(Id oppId : omfMap.keySet()){
    		
    		//IntentionalProductMonthFollow__c omf = getLastestOmf(omfMap.get(oppId));
    		IntentionalProductMonthFollow__c tmp = null;
    		for(IntentionalProductMonthFollow__c omf : omfMap.get(oppId)){
	    		if(tmp == null) tmp = omf;
	    		if(omf.Year__c == null ||omf.Month__c == null )continue;
    		    if(omf.LastFollowup__c == true )omfset.add(omf.Id);
	    		tmp =(omf.Year__c.compareTo(tmp.Year__c) > 0  || omf.Year__c.compareTo(tmp.Year__c) < 0) ? ( omf.Year__c.compareTo(tmp.Year__c)  > 0 ? omf : tmp  ) :(getMonth(omf.Month__c)  > getMonth(tmp.Month__c) ? omf : tmp ) ;
	    		
	    	}
            System.debug('IntentionalProductMonthFollowHandler==>'+tmp);
    		if(tmp == null || tmp.LastFollowup__c == true ){
                newomfset.add(tmp.Id);
                continue;
            }
    		omfList.add(new IntentionalProductMonthFollow__c(Id=tmp.Id,LastFollowup__c = true));
    		newomfset.add(tmp.Id);

    	}
         System.debug('IntentionalProductMonthFollowHandler');
         System.debug(omfMap);
         System.debug(newomfset);
         System.debug(omfset);
    	for(Id  omfId : omfset ){
    		if( newomfset.size() > 0 &&  !newomfset.contains(omfId))omfList.add(new IntentionalProductMonthFollow__c(Id =omfId,LastFollowup__c = false));

    	}
    	if(omfList.size() > 0) update omfList;


    } 
     private static Integer getMonth(String m){
        if(m == null) return null;
        try{
           return  m.length() > 1 ? Integer.valueOf( m.substring(0,m.length() -1)): Integer.valueOf(m);
        }catch(Exception e ){
            return null;
        }
    }
    */


}